<?php
namespace iBrand\Shop\Core\Repositories;

use iBrand\Shop\Core\Models\ShopOrder;
use iBrand\Shop\Core\Models\Order;
use Prettus\Repository\Eloquent\BaseRepository;
use Carbon\Carbon;


class ShopOrderRepository extends BaseRepository
{

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return ShopOrder::class;
    }

    public function createInfo($order_id, $shop_id, $clerk_id, $value)
    {

        if (!$res = $this->model->where('order_id', $order_id)->first()) {
            return $this->model->create([
                'order_id' => $order_id,
                'shop_id' => $shop_id,
                'clerk_id' => $clerk_id,
                'value' => $value
            ]);
        };
        return $res;
    }


    public function getBonusTodayByClerkID($clerk_id)
    {
        $time = Carbon::now();
        $date = Carbon::createFromTimestamp($time->timestamp);
        $today = Carbon::today()->timestamp;
        $tomorrow = Carbon::tomorrow()->timestamp;


        $value = $this->model->where('clerk_id', $clerk_id)->where('value', '>', 0)
            ->with('order')
            ->whereHas('order', function ($query) use ($today, $tomorrow) {
                return $query->whereIn('status', [Order::STATUS_RECEIVED, Order::STATUS_COMPLETE])
                    ->where('created_at', '>=', Carbon::createFromTimestamp($today))->where('created_at', '<', Carbon::createFromTimestamp($tomorrow));

            })->sum('value');

        if ($value == null) {
            return 0;
        }
        return number_format($value / 100, 2, ".", "");
    }


    public function getBonusByClerkID($clerk_id)
    {

        $value = $this->model->where('clerk_id', $clerk_id)->where('value', '>', 0)
            ->with('order')
            ->whereHas('order', function ($query) {
                return $query->whereIn('status', [Order::STATUS_RECEIVED, Order::STATUS_COMPLETE]);
            })->sum('value');

        if ($value == null) {
            return 0;
        }
        return number_format($value / 100, 2, ".", "");
    }


    public function getBonusOrderList($clerk_id)
    {
        return $this->model->where('clerk_id', $clerk_id)->where('value', '>', 0)->orderBy('created_at', 'desc')
            ->with('order')
            ->whereHas('order', function ($query) {
                return $query->whereIn('status', [Order::STATUS_RECEIVED, Order::STATUS_COMPLETE]);
            })->paginate(10);


    }


    public function getOrderInfoByClerk($clerk, $order_id)
    {

        if ($clerk->is_clerk_owner) {
            return $this->model->where('shop_id', $clerk->shop_id)->where('order_id', $order_id)
                ->with('order')
                ->whereHas('order', function ($query) {
                    return $query;
                })->first();
        }

        return $this->model->where('clerk_id', $clerk->id)->where('order_id', $order_id)
            ->with('order')
            ->whereHas('order', function ($query) {
                return $query;
            })->first();

    }


    /**
     * 统计店员当天订单总数
     * @param $clerk_id
     * @return mixed
     */
    public function getOrderCountTodayByClerkID($clerk_id)
    {
        $today = Carbon::today()->timestamp;
        $tomorrow = Carbon::tomorrow()->timestamp;
        return $this->model->where('clerk_id', $clerk_id)
            ->where('created_at', '>=', date('Y-m-d', $today))
            /* ->where('created_at', '<', date('Y-m-d H:i:s', $tomorrow))*/
            ->with('order')
            ->whereHas('order', function ($query) {
                return $query->whereIn('status', [Order::STATUS_RECEIVED, Order::STATUS_COMPLETE]);
            })->count();
    }


    /**
     * 统计店员近N天销售
     * @param $clerk_id
     * @param $day
     * @return mixed
     */
    public function getOrderCountDayByClerkID($clerk_id, $day)
    {
        $today = Carbon::today()->timestamp;
        $tomorrow = Carbon::tomorrow()->timestamp;
        return $this->model->where('clerk_id', $clerk_id)
            ->where('created_at', '>=', date('Y-m-d', strtotime("$day day", $today)))
            ->where('created_at', '<', date('Y-m-d', $tomorrow))
            ->with('order')
            ->whereHas('order', function ($query) {
                return $query->whereIn('status', [Order::STATUS_RECEIVED, Order::STATUS_COMPLETE]);
            })->count();
    }


}
